<?php
##############################################
#
#   用户登入
# @ Time : 2017-2-9 - 下午10:10:03
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Home\Controller;
use Think\Controller;
use Home\Model\UserModel;

class LoginController extends Controller{
	
	public function index(){
		$this -> display('index');
	}
	//用户：账号：1085563810@qq.com 密码jiang1234
	//用户登入
	public function login(){
		if(!IS_POST) $this -> error('非法请求');
		$data['username'] = I('post.username');
		$data['password'] = md5(I('post.password')); 
		$user_model = new UserModel();
		$user_info = $user_model -> getUser($data);
		if($user_info){
			if(isset($_POST['auto_login'])){
				$username = $user_info['uname'];
				$ip = get_client_ip();
				$value = encryption($username.'|'.$ip);
				setcookie('auto_login', $value, C('AUTO_LOGIN_TIME'), '/');
			}
			$_SESSION['uid'] = $user_info['uid'];
			$_SESSION['username'] = $user_info['uname'];
			$this -> success('登入成功', U('Index/index'), 2);
		}else{
			$this -> error('登入失败', '', 2);
		}		
	}
	
	/*
	 * 用户退出
	 */
	public function quit(){
		unset($_SESSION['uid'], $_SESSION['username']);
		setcookie('auto_login', '' ,time()-1, '/');
		$this -> success('退出成功', U('Index/index'), 2);
	}
	
}